<script setup lang="ts">
import { Handle, Position, useVueFlow } from '@vue-flow/core'
import { computed,ref, watch } from 'vue'
const props = defineProps<{
  id: string;
  data: {
    name: string;
    route?: string;
    timeout?: number;
    isOutputArgs?: boolean;
  };
  style?: Record<string, any>; // 添加 style 属性
}>()

const { updateNodeData,} = useVueFlow()

const name = computed({
  get: () => props.data.name,
  set: (name) => updateNodeData(props.id, {...props.data, name: name }),
})

import useConnectionValid from './ts/connectionValid';
const { isValidConnection } = useConnectionValid();

// 节点属性
const route = ref(props.data.route);
const timeout = ref(props.data.timeout);
watch(() => [route.value, timeout.value], (newValue) => {
  updateNodeData(props.id, {...props.data, route: route.value, timeout: timeout.value })
});
</script>

<template>
    <div class="AgentName"> <input :id="`${id}-input`" v-model="name"  class="nodrag"> </div>
    <div class="AgentContent">
      <Handle id="flow-in" type="target" :class="['AgentFlowInputHandle','top-0']" :position="Position.Left" title="FLow In"  :connectable="()=>{return isValidConnection(props.id,'flow-in', 'target')}" ><span class="HandleLeftSpan">Flow In</span></Handle>
      <Handle id="function-call" type="target" :class="['AgentToolCall','top-1']" :position="Position.Left" title="Call"   :connectable="()=>{return isValidConnection(props.id,'function-call', 'target')}"><span class="HandleLeftSpan" >Call</span></Handle>
      <Handle id="tool" type="target" :class="['AgentTool','top-2']" :position="Position.Left" title="Tool"   :connectable="()=>{return isValidConnection(props.id,'tool', 'target')}"><span class="HandleLeftSpan" >Tool</span></Handle>
      

      <Handle id="flow-noexecute-next" type="source" :class="['AgentFlowOutputHandle','top-0']" :position="Position.Right"  :connectable="()=>{return isValidConnection(props.id,'flow-next','source')}" title="No Execute Flow Out" ><span class="HandleRightSpan">No Execute</span></Handle>
      <Handle id="flow-successed-next" type="source" :class="['AgentFlowOutputHandle','top-1']" :position="Position.Right"  :connectable="()=>{return isValidConnection(props.id,'flow-next','source')}" title="Success Flow Out" ><span class="HandleRightSpan">Success</span></Handle>
      <Handle id="flow-failed-next" type="source" :class="['AgentFlowOutputHandle','top-2']" :position="Position.Right"  :connectable="()=>{return isValidConnection(props.id,'flow-next','source')}" title="Fail Flow Out" ><span class="HandleRightSpan">Fail</span></Handle>
      <Handle id="result-msg" type="source" :class="['AgentMsg','top-3']" :position="Position.Right"  :connectable="()=>{return isValidConnection(props.id,'result-msg','source')}" title="Result" ><span class="HandleRightSpan">Result</span></Handle>
      <div class="NodeData">
        <!-- 第一行：textinput -->
        <div class="input-row">
          <label for="textInput">Route:</label>
          <input
            id="textInput"
            type="text"
            v-model="route"
            placeholder="/your-route"
            :class="['text-input','nodrag']"
          />
        </div>
        <div class="input-row">
          <label for="textInput">Timeout:</label>
          <input
            id="textInput"
            type="number"
            v-model="timeout"
            placeholder="3000"
            :class="['text-input','nodrag']"
          />
        </div>
      </div>
    </div>

</template>


<style scoped>

.AgentName {
  position: relative;
  display: inline-block;
  font-size: 14px;
  width: 100%;
  padding-left: 70px;
  padding-right: 70px;
  text-align: center;
}

.AgentName input {
  max-height: 20px;
  height: 100%;
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  color: #333;
  font-size: 16px;
  padding: 8px 12px;
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
  text-align: center;
  background-color: #00000000;
}

/* 输入框聚焦时 */
.AgentName input:focus {
  background-color: #f5f5f5;
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.5);
  color: #000;
}

/* 输入框没有内容时（placeholder显示） */
.AgentName input:placeholder-shown {
  color: #ffffff;
}

/* 输入框内容区域边距 */
.AgentName input::placeholder {
  color: #ccc;
  font-style: italic;
}

/* 输入框平滑过渡效果 */
.AgentName input {
  transition: background-color 0.3s, box-shadow 0.3s, color 0.3s;
}
.AgentContent{
  height: 100%;
  width: 100%;
}
</style>